package cn.didi.feidi.androidorder.assembly;

import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.databinding.DialogInputPasswordBinding;

/**
 * Created by jinxiong on 2017/8/29.
 */

public class FeiDiInputPasswordFragmentDialog extends BaseDialogFragment {

    private static final int MAX_LENGTH = 6;//输入的最大长度
    private static final String TAG = "FeiDiInpuPasswordFragme";
    DialogInputPasswordBinding binding;
    StringBuilder inputNumber = new StringBuilder();
    View.OnClickListener forgetPassworListener;
    Callback callback;
    boolean noCloseActivity;
    View.OnClickListener closeDialogListener;
    CloseActivityCallback mCloseActivityCallback;

    public FeiDiInputPasswordFragmentDialog setCloseDialogListener(View.OnClickListener closeDialogListener) {
        this.closeDialogListener = closeDialogListener;
        return this;
    }

    public FeiDiInputPasswordFragmentDialog setNoCloseActivity(boolean noCloseActivity) {

        this.noCloseActivity = noCloseActivity;
        return this;
    }

    public FeiDiInputPasswordFragmentDialog setCloseActivityCallback(CloseActivityCallback closeActivityCallback) {
        mCloseActivityCallback = closeActivityCallback;
        return this;
    }

    public FeiDiInputPasswordFragmentDialog setForgetPassworListener(View.OnClickListener forgetPassworListener) {
        this.forgetPassworListener = forgetPassworListener;
        return this;
    }

    public FeiDiInputPasswordFragmentDialog setCallback(Callback callback) {
        this.callback = callback;
        return this;
    }

    @NonNull
    @Override
    protected Dialog newDialog() {

        Dialog dialog = new Dialog(getActivity(), R.style.style_dialog);
        dialog.setCanceledOnTouchOutside(false);
        binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.dialog_input_password,
                (ViewGroup) getActivity().getWindow().getDecorView(), false);
        dialog.setContentView(binding.getRoot());
        setDialogGravity(Gravity.BOTTOM).setRightPadding(0).setLeftPadding(0);
        return dialog;
    }

    @Override
    protected void addListener() {
        binding.inputPasswordKeyboard.setCallback(new FeiDiKeyboardTextView.Callback() {
            @Override
            public void callback(char number) {

                ((TextView) binding.dialogInputPasswordParentLl.getChildAt(inputNumber.length())).setText(String.valueOf(number));

                inputNumber.append(number);

                if (inputNumber.length() == MAX_LENGTH) {
                    if (callback != null) {
                        callback.inputCallback(inputNumber.toString());
                    }
                }

                Log.d(TAG, "inputCallback: " + inputNumber.toString());
            }
        });

        binding.inputPasswordKeyboard.setClearListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int index = inputNumber.length() - 1;
                if (index >= 0) {
                    ((TextView) binding.dialogInputPasswordParentLl.getChildAt(index)).setText("");
                    inputNumber.deleteCharAt(index);
                }
                Log.d(TAG, "onClick: " + inputNumber.toString());

            }
        });


        if (closeDialogListener != null) {
            binding.dialogInputPasswordCloseFl.setOnClickListener(closeDialogListener);
        } else {

            binding.dialogInputPasswordCloseFl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
        binding.dialogInputPasswordForgetPasswordTv.setOnClickListener(forgetPassworListener);
    }

    @Override
    protected void initView() {

    }

    /**
     * 清空输入框
     */
    public void clearInput() {
        int index = inputNumber.length() - 1;
        while (index >= 0) {
            ((TextView) binding.dialogInputPasswordParentLl.getChildAt(index)).setText("");
            inputNumber.deleteCharAt(index);
            index--;
        }
    }

    @Override
    public void dismiss() {
        noCloseActivity = true;
        super.dismiss();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (noCloseActivity) {
            return;
        }
        if (mCloseActivityCallback != null) {
            mCloseActivityCallback.closeCallback();
        }
    }


    public interface CloseActivityCallback {
        void closeCallback();
    }

    public interface Callback {
        void inputCallback(String inputString);
    }
}
